package net.bolue.sysmng.util;

import net.bolue.sysmng.db.metadata.ColMetaData;
import net.bolue.sysmng.tblbean.ColInfo;

/**
 * 数据库元素工具类
 * @author Administrator
 *
 */
public class DbMetaUtil {

	/**
	 * 将表列元素转化为表列信息
	 * @param colMetaData
	 * @return
	 */
	public static ColInfo metaToCol(ColMetaData colMetaData){
		ColInfo colInfo = new ColInfo();
		String columnName = colMetaData.getColumnName();
		colInfo.setColName(columnName);
		String colDesc = columnName.toUpperCase();
		colInfo.setColDesc(colDesc);
		Integer jType = parseJType(colMetaData.getDataType(),colMetaData.getColumnSize());
		colInfo.setJType(jType);
		colInfo.setIsPkey(0);
		if (colMetaData.getNullable() == 0){
			colInfo.setIsNullAble(0);
		}else{
			colInfo.setIsNullAble(1);
		}
		colInfo.setDicKind(0);
		if(jType==4){
			colInfo.setColSize(19);
		}else{
			colInfo.setColSize(colMetaData.getColumnSize());
		}
		Integer dispLen = 0;
		if ((jType == 5) || (jType == 1) || (jType == 2)){
			dispLen = 20;
		}else if ((jType < 50) && (jType > 0)){
			dispLen = 20;
		}else{
			dispLen = 50;
		}
		colInfo.setDispLen(dispLen);
		colInfo.setTagType(1);
		colInfo.setDecDigits(0);
		return colInfo;
	}
	
	/**
	 * 通过字段类型和长度解析数据类型
	 * @param dataType
	 * @param columnSize
	 * @return
	 */
	public static Integer parseJType(Integer dataType,Integer columnSize){
		int i = 3;
		switch (dataType){
			case -5:
				i = columnSize < 10 ? 1 : 2;
				break;
			case -2:
				i = 6;
				break;
			case -7:
				i = 3;
				break;
			case 3:
				if (columnSize>0){
					i = columnSize < 10 ? 1 : 2;
				}else{
					i = 5;
				}
				break;
			case 91:
				i = 4;
				break;
			case 8:
				i = 5;
				break;
			case 6:
				i = 5;
				break;
			case 4:
				i = columnSize < 10 ? 1 : 2;
				break;
			case -4:
				i = 6;
				break;
			case -1:
				i = 7;
				break;
			case 2:
				if (columnSize > 0){
					i = columnSize < 10 ? 1 : 2;
				}else {
					i = 5;
				}
				break;
			case 7:
				i = 5;
				break;
			case 5:
				i = 1;
				break;
			case 92:
				i = 4;
				break;
			case 93:
				i = 4;
				break;
			case -6:
				i = 1;
				break;
			case 2004:
				i = 6;
				break;
			case 2005:
				i = 7;
				break;
			default:
				i = 3;
		}
		return i;
	}
}
